#pragma once
#include <iostream>
#include <vector>
#include <boost/algorithm/string.hpp>
#include <fstream>
#include <unordered_map>
#include "cppjieba/Jieba.hpp"
namespace ns_util
{
    class FileUtil
    {
    public:
        static bool ReadFile(const std::string &file_path, std::string *out)
        {
            // 读取文件
            std::fstream in(file_path, std::ios::in);
            if (!in.is_open())
            {
                std::cerr << "open: " << file_path << "error " << std::endl;
                return 0;
            }
            std::string line;
            while (std::getline(in, line))
            {
                *out += line;
            }
            in.close();
            return true;
        }
    private:
    };

    //对字符串做分词处理
    class StringUtil
    {
    public:
        // 根据sep分割line中内容,填入result中
        static void Split(const std::string line, std::vector<std::string> *result, const std::string &sep)
        {
            boost::split(*result, line, boost::is_any_of(sep), boost::token_compress_on);
        }
    };
    const char *const DICT_PATH = "./dict/jieba.dict.utf8";
    const char *const HMM_PATH = "./dict/hmm_model.utf8";
    const char *const USER_DICT_PATH = "./dict/user.dict.utf8";
    const char *const IDF_PATH = "./dict/idf.utf8";
    const char *const STOP_WORD_PATH = "./dict/stop_words.utf8";
    class JiebaUtil
    {
    private:
        // 构建jieba静态对象
        static cppjieba::Jieba jieba;
        std::unordered_map<std::string, bool> stop_words;
    private:
        JiebaUtil() 
        {
        }
        JiebaUtil(const JiebaUtil &) = delete; // 禁止拷贝构造
        static JiebaUtil *instance;            // 为什么？
    public:
        // 分割字符串，关键字并放入out
        static void CutString(const std::string &src, std::vector<std::string> *out)
        {
            jieba.CutForSearch(src, *out);
        }
    };
    //静态对象
    JiebaUtil *JiebaUtil::instance = nullptr;
    //静态对象初类外始化
    cppjieba::Jieba JiebaUtil::jieba(DICT_PATH, HMM_PATH, USER_DICT_PATH, IDF_PATH, STOP_WORD_PATH);
}